package com.dragon.concurrent.demo.cas;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author dragon
 * @version V1.0.0-RELEASE
 * @date 2019/6/2 19:34
 */
public class AtomicReferenceTest {
    public static final AtomicReference<String> atomicStr = new AtomicReference<>("abc");

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                try {
                    Thread.sleep(Math.abs((int) (Math.random() * 100)));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (atomicStr.compareAndSet("abc", "dragon")) {
                    System.out.println("Thread:" + Thread.currentThread().getId() + "SUCCESS");
                } else {
                    System.out.println("Thread:" + Thread.currentThread().getId() + "FAILURE");
                }
            }).start();
        }
    }
}
